package musicstore.dao.impl;

import musicstore.dao.AlbumDao;
import musicstore.dao.ShoppingCartDao;
import musicstore.entity.Album;
import musicstore.entity.CartItem;
import musicstore.entity.ShoppingCart;

public class ShoppingCartDaoImpl implements ShoppingCartDao {

	@Override
	public void addItemToCart(ShoppingCart cart, int albumId) {
		CartItem c = findCartItem(cart, albumId);
		if(c != null)
			c.setQuantity(c.getQuantity()+1);
		else{
			AlbumDao dao = new AlbumDaoImpl();
			Album a = dao.getAlbumById(albumId);
			System.out.println(a.getTitle());
			CartItem item = new CartItem();
			item.setAlbumId(albumId);
			item.setQuantity(1);
			item.setTitle(a.getTitle());
			item.setUnitPrice(a.getPrice());
			cart.getItems().add(item);
		}
	}

	@Override
	public void removeItemToCart(ShoppingCart cart, int albumId) {
		CartItem c = findCartItem(cart, albumId);
		if(c != null)
			cart.getItems().remove(c);
	}

	private CartItem findCartItem(ShoppingCart cart ,int albumId){
		for(CartItem c : cart.getItems()){
			if(c.getAlbumId()==albumId)
				return c;
		}
		return null;
	}
}
